﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Data;
using Microsoft.Windows.Controls;
using System.Collections.ObjectModel;
using Negocio;

namespace Presentacion
{
    public partial class WPlayersEnterData
    {
        private int selectedItem;
        private ObservableCollection<PlayerExecution> playerExeclist;
        private DataTable table;
        private PlayerExecution playerExecution;
        public WPlayersEnterData(int OneSelectedItem, ObservableCollection<PlayerExecution> OnePlayerExecList)
        {
            this.InitializeComponent();
            table = new DataTable();
            selectedItem = OneSelectedItem;
            if (selectedItem != -1)
            {
                playerExeclist = OnePlayerExecList;
                dpDate.SelectedDate = DateTime.Today;
                dpDate.IsEnabled = false;
                cbxPlayer.ItemsSource = playerExeclist;
                playerExecution = playerExeclist[selectedItem];
                loadtable(playerExecution);
                labelNombreEvaluacion.Content = playerExeclist[selectedItem]
    .EvaluationName;
                txbComments.Text = playerExeclist[selectedItem].Comments;
            }                        
        }

        private void loadtable(PlayerExecution plyExecution)
        {
            table.Clear();
            table.Columns.Clear();
            dataGrid1.ItemsSource = null;
            DataColumn col = new DataColumn("Prueba", typeof(string));
            col.ReadOnly = true;
            table.Columns.Add(col);
            DataColumn colValue = new DataColumn("Valor", typeof(float));
            table.Columns.Add(colValue);
            dataGrid1.ItemsSource = table.DefaultView;

            for (int i = 0; i < plyExecution.Values.Count; i++)
            {
                if (plyExecution.Values[i].Test.Sequence.Equals("n/a"))
                {
                    DataRow row = table.NewRow();
                    row["Prueba"] = plyExecution.Values[i].Test.Name;
                    row["Valor"] = plyExecution.Values[i].ValueExecution;
                    DataGridComboBoxColumn combo = new DataGridComboBoxColumn();
                    table.Rows.Add(row);
                }
            }
            dataGrid1.ItemsSource = table.DefaultView;
            dataGrid1.ColumnWidth = 217;
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < playerExecution.Values.Count; j++)
                {
                    if (table.Rows[i]["Prueba"].Equals(playerExecution.Values[j].Test.Name))
                    {
                        playerExecution.Values[j].ValueExecution = float.Parse(table.Rows[i]["Valor"].ToString());
                        j = playerExecution.Values.Count;
                    }
                }

            }
            Club.getInstance().calculateValuesOfExecution(playerExecution);
            Club.getInstance().updatePlayerExecution(playerExecution.EvaluationName, playerExecution.GroupName, playerExecution.Player, playerExecution.Values, playerExecution.Date, txbComments.Text, playerExecution);
            //playerExecution.updated();
            MessageBox.Show("Se ingresaron los datos", "Atención", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void cbxPlayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadtable((PlayerExecution)cbxPlayer.SelectedItem);
            playerExecution = null;
            playerExecution=(PlayerExecution)cbxPlayer.SelectedItem;
            txbComments.Text = playerExecution.Comments;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid1.SelectedItem != null)
            {
                dgReferenceMeasures.ItemsSource = null;
                for (int i = 0; i < playerExecution.Values.Count; i++)
                {
                    DataRowView row = (DataRowView)dataGrid1.SelectedItem;
                    if (playerExecution.Values[i].Test.Name.Equals(row["Prueba"].ToString()))
                    {
                        dgReferenceMeasures.ItemsSource = playerExecution.Values[i].Test.ReferenceMeasures;
                    }
                }
            }
        }
    }
}